<?php

namespace app\adminapi\controller\v1\order;

use app\adminapi\controller\AuthController;
use app\adminapi\export\OrderExport;
use app\services\order\OrderService;
use think\facade\App;
use app\model\distributor\Distributor;
use app\model\other\Express;

/**
 * 订单管理
 * Class Order
 * @package app\adminapi\controller\v1\order
 */
class Order extends AuthController
{
    public function __construct(App $app, OrderService $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    /**
     * 订单列表
     * @return \think\Response
     */
    public function list(){

        $params = $this->request->getMore([
            ['start_time', 0],
            ['end_time', 0],
            ['product_name',''],
            ['dstatus',-1],
            ['order_type',0],
            ['sn',''],
            ['buyer_phone',''],
            ['adder_phone',''],
            ['pay_type',0],
            ['distributor_id',0]
        ]);

        $result = $this->services->getOrderList($params);
        return app('json')->success($result);
    }

    /**
     * 订单导出
     * @return mixed
     */
    public function export(){
        $params = $this->request->getMore([
            ['start_time', 0],
            ['end_time', 0],
            ['product_name',''],
            ['dstatus',-1],
            ['order_type',0],
            ['sn',''],
            ['buyer_phone',''],
            ['adder_phone',''],
            ['pay_type',0],
            ['distributor_id',0]
        ]);

        return app()->make(OrderExport::class)->export($params);
    }

    /**
     * 订单详情
     * @return \think\Response
     */
    public function detail(){
        $params = $this->request->getMore([
            ['order_id',0]
        ]);

        $result = $this->services->orderDetail($params);
        return app('json')->success($result);
    }

    /**
     * 取消订单
     * @return \think\Response
     */
    public function cancel(){
        $params = $this->request->getMore([
            ['order_id',0]
        ]);
        $this->services->cancel($params);
        return app('json')->success(100019);
    }

    /**
     * 发货填写快递单号
     * @return \think\Response
     */
    public function shipping(){
        $params = $this->request->postMore([
            ['order_id',0],
            ['shipping_code',''],
            ['express_id',0],
            ['product_attr_value_unique',''],
        ]);

        $this->services->shipping($params);
        return app('json')->success(410273);
    }

    /**
     * 卖家备注
     * @return \think\Response
     */
    public function desc(){
        $params = $this->request->postMore([
            ['order_id',0],
            ['order_desc',''],
        ]);

        $this->services->desc($params);
        return app('json')->success(100024);
    }

    /**
     * 售后备注
     * @return \think\Response
     */
    public function remark(){
        $params = $this->request->postMore([
            ['order_id',0],
            ['remark',''],
            ['product_attr_value_unique',''],
        ]);

        $this->services->remark($params);
        return app('json')->success(100024);
    }

    /**
     * 经销商列表
     * @return \think\Response
     */
    public function distributou(){

        return app('json')->success(Distributor::field(['id','distributor_name'])->select()->toArray());
    }

    /**
     * 快递公司列表
     * @return \think\Response
     */
    public function express(){
        return app('json')->success(Express::where(['is_show'=>1,'status'=>1])->field(['id','name'])->select()->toArray());
    }
}